package com.college.utils;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.college.entity.Result;

public class JSONTools {
	public static String toJsonString(Object object, String... params) {
		List<String> list = toList(params);
		Object object2 = JSON.parse(JSON.toJSONString(object));
		filterParam(object2, list);
		return object2.toString();
	}

	public static List<String> toList(String... params) {
		List<String> list = new LinkedList<String>();
		for (String string : params) {
			list.add(string);
		}
		return list;
	}

	private static void filterParam(Object object, List<String> list) {
		if (object instanceof JSONArray) {
			JSONArray jsonArray = (JSONArray) object;
			for (int i = 0; i < jsonArray.size(); i++) {
				filterParam(jsonArray.get(i), list);
			}
		} else if (object instanceof JSONObject) {
			JSONObject jsonObject = (JSONObject) object;
			Set<Entry<String, Object>> set = jsonObject.entrySet();
			Iterator<Entry<String, Object>> iterator=set.iterator();
			while (iterator.hasNext()) {
				Entry<String, Object> entry=iterator.next();
				if (list.contains(entry.getKey())) {
					iterator.remove();
				}else {
					Object object2=entry.getKey();
					filterParam(object2, list);
				}
			}
		}
	}
}
